
const db = require('../db/index')
const path = require('path')
// const { date, func } = require('joi')

// 获取用户信息函数
exports.get_userInfo = function (req, res) {
    // console.log('ok');
    db.query('select * from student where student_num=?', req.user.username, function (err, results) {
        if (err) return res.cc(err)
        if (results.length < 1) return res.cc('获取失败')
        res.send({
            status: 0,
            message: '获取成功',
            data: results
        })
    })
}

//更新用户信息(有问题)
exports.update_userInfo = function (req, res) {
    // console.log(req.body);
    if (!req.body.phone && !req.body.email) {
        res.cc('请输入修改内容')
    }
    if (req.body.phone && !req.body.email) {
        db.query('update student set phone=? where student_num=?', [req.body.phone, req.user.username], function (err, results) {
            if (err) return res.cc(err)
            if (results.affectedRows !== 1) return res.cc('修改失败')
            res.send({
                status: 0,
                message: '修改成功'
            })
        })
    }
    if (!req.body.phone && req.body.email) {
        db.query('update student set email=? where student_num=?', [req.body.email, req.user.username], function (err, results) {
            if (err) return res.cc(err)
            if (results.affectedRows !== 1) return res.cc('修改失败')
            res.send({
                status: 0,
                message: '修改成功'
            })
        })
    }
    if (req.body.phone && req.body.email) {
        db.query('update student set ? where student_num=?', [{ phone: req.body.phone, email: req.body.email }, req.user.username], function (err, results) {
            if (err) return res.cc(err)
            if (results.affectedRows !== 1) return res.cc('修改失败')
            res.send({
                status: 0,
                message: '修改成功'
            })
        })
    }
}

//修改用户密码
exports.update_password = function (req, res) {
    // res.send('ok')
    db.query('select * from user where id=?', req.user.id, function (err, results) {
        if (err) return res.cc(err)
        if (results.length !== 1) return res.cc('查询失败')
        if (req.body.oldpassword !== results[0].password)
            return res.cc('原密码错误')
        db.query('update user set password=? where id=?', [req.body.newpassword, req.user.id], function (err, results) {
            if (err) return res.cc(err)
            if (results.affectedRows !== 1) return res.cc('修改失败')
            return res.send({ status: 0, message: '密码修改成功' })
        })
    })
}

//修改头像
exports.update_avater = function (req, res) {
    res.send('ok')
}

//获取公告信息
exports.get_notice = function (req, res) {
    db.query('select * from notice where dele_status=0', function (err, results) {
        if (err) return res.cc(err)
        if (results.length < 1) return res.cc('没有查询到信息')
        res.send({
            status: 0,
            message: '查询成功',
            data: results
        })
    })
}

//获取已选课题信息
exports.get_topic = function (req, res) {
    // console.log(req.user.username);
    db.query('select * from topic where student_num=?', req.user.username, function (err, results) {
        if (err) return res.cc(err)
        if (results.length < 1) return res.cc('没有选择课题')
        console.log(results[0].teacher_id);
        db.query('select * from teacher where teacher_num=?', results[0].teacher_id, function (err, results1) {
            if (err) return res.cc(err)
            if (results1.length < 1) return res.cc('没有教师信息')
            res.send({
                status: 0,
                message: '获取成功',
                data: [results1, results]
            })
        })
    })
}

//获取所有课题信息
exports.get_allTopic = function (req, res) {
    db.query('select * from student where student_num=?', req.user.username, function (err, result) {
        if (err) return res.cc(err)
        if (result.length < 1) return res.cc('没有学生信息')
        db.query('select * from topic where topic_status=0 and pass=1 and  collage=? and student_num is null', result[0].student_college, function (err, results) {
            if (err) return res.cc(err)
            if (results.length < 1) return res.cc('没有课题信息')
            // console.log(results[0].teacher_id);
            var list = []
            for (var i = 0; i < results.length; i++) {
                list.push(results[i].teacher_id)
            }
            db.query('select * from teacher where teacher_num in (?)', [list], function (err, results1) {
                if (err) return res.cc(err)
                if (results1.length < 1) return res.cc('查询失败')
                results.forEach(function (val) {
                    results1.forEach(function (val1) {
                        if (val.teacher_id == val1.teacher_num)
                            // 往对象添加值
                            Object.assign(val, { teacher_tid: val1.id, teacher_name: val1.teacher_name });
                    });
                });
                //总条数
                const total = results.length
                // console.log(results);
                // 当前页数
                const pageCurrent = req.query.current
                // 一页多少条
                const pageSize = req.query.limit
                // 一共多少页
                const pageCount = Math.ceil(total / pageSize)

                const datalist = results.slice((pageCurrent - 1) * pageSize, pageCurrent * pageSize)
                // console.log(datalist);
                res.send({
                    status: 0,
                    message: '查询成功',
                    data: { datalist, total }
                })
            })
        })
    })
}

// 查看课题信息
exports.get_topicinfo = function (req, res) {
    // console.log(req.query);
    db.query('select * from topic where id=?', req.query.id, function (err, results) {
        if (err) return res.cc(err)
        if (results.length < 1) return res.cc('没有查看到课题信息')
        // console.log(results[0].teacher_id);
        db.query('select * from teacher where teacher_num=?', results[0].teacher_id, function (err, result) {
            if (err) return res.cc(err)
            if (results.length < 1) return res.cc('查询错误')
            res.send({
                status: 0,
                message: '查看成功',
                data: [results, result]
            })

        })
    })
}

//上传开题报告
exports.add_report = function (req, res) {
    db.query('select teacherId from teacher_student where studentId=?', req.user.username, function (err, results) {
        if (err) return res.cc(err)
        if (results.length < 1) res.cc('无法获取信息')
        // 判断验证req.file
        if (!req.file || req.file.fieldname !== 'file')
            return res.cc('文件必上传')
        //处理文件的信息对象
        const articleInfo = {
            //标题，内容，发布状态，所属内容
            report_status: 0,
            teacher_num: results[0].teacherId,
            student_num: req.user.username,
            report_name: req.file.originalname,
            my_report: path.join('/upload', req.file.filename)
        }
        db.query('insert into report set ?', articleInfo, function (err, results) {
            if (err) return res.cc(err)
            if (results.affectedRows != 1) return res.cc('发布失败')
            res.send({
                status: 0,
                message: '发布成功',
            })
        })
    })
}


//获取指导记录
exports.get_direct = function (req, res) {
    // console.log(req.user.username);
    db.query('select * from direct where student_num=?', req.user.username, function (err, results) {
        if (err) return res.cc(err)
        if (results.length < 1) return res.cc('没有指导记录存在')
        res.send({
            status: 0,
            message: '得到了指导记录',
            data: results
        })
    })
}


//展示指导记录
exports.show_direct = function (req, res) {
    // console.log(req.user.username);
    // console.log(req.query);
    db.query('select * from direct where id=?', req.query.id, function (err, results) {
        if (err) return res.cc(err)
        if (results.length < 1) return res.cc('没有指导记录存在')
        res.send({
            status: 0,
            message: '得到了指导记录',
            data: results
        })
    })
}

//获取教师留言信息
exports.get_message = function (req, res) {
    db.query('select * from respondmessage where student_num=?', req.user.username, function (err, results) {
        if (err) return res.cc(err)
        if (results.length < 1) return res.cc('没有留言信息')
        res.send({
            status: 0,
            message: '教师留言来了',
            data: results
        })
    })
}


//学生发送留言
exports.post_message = function (req, res) {
    // console.log(req.user.username);
    console.log(req.body);
    db.query('select * from teacher_student where studentId=?', req.user.username, function (err, results) {
        if (err) return res.cc(err)
        if (results.length < 1) return res.cc('你还没有选择老师呢')
        // console.log(results[0].teacherId);
        db.query('insert into message set ?', { student_num: req.user.username, message_content: req.body.message_content, message_date: new Date() }, function (err, result) {
            if (err) return res.cc(err)
            if (results.affectedRows < 1) return res.cc('留言插入失败')
            res.send({
                status: 0,
                message: '插入成功了哦',
            })
        })
    })
}

//获取教师信息
exports.get_teacherinfo = function (req, res) {
    // console.log(req.query);
    db.query('select * from teacher where id=?', req.query.id, function (err, result) {
        if (err) return res.cc(err)
        if (result.length < 1) return res.cc('查询教师信息失败')
        // console.log(result);
        res.send({
            status: 0,
            message: '获取教师信息成功',
            data: result
        })
    })
}

//选择课题按钮
exports.send_topic = function (req, res) {
    db.query('select * from topic where student_num=?', req.user.username, function (err, result) {
        if (err) return res.cc(err)
        if (result.length > 0) return res.cc('你已经选择了')
        db.query('update topic set ? where id=?', [{ student_num: req.user.username, topic_status: 0 }, req.body.id], function (err, results) {
            if (err) return res.cc(err)
            if (results.affectedRow < 1) return res.cc('修改失败')
            res.send({
                status: 0,
                message: '选题成功',
            })
        })
    })
}

//获取个人任务书
exports.choose_task = function (req, res) {
    db.query('select * from task where student_num=?', req.user.username, function (err, results) {
        if (err) return res.cc(err)
        if (results.length < 1) return res.cc('没有发布任务书哦')
        res.send({
            status: 0,
            message: '任务书来了',
            data: results
        })
    })
}

//获取开题报告信息 
exports.get_report = function (req, res) {
    db.query('select * from report where student_num=?', req.user.username, function (err, results) {
        // console.log(results[0].reply_teacher);
        if (err) return res.cc(err)
        if (results.length < 1) return res.cc('还没有提交开题报告哦')
        // console.log(results);
        if (!results[0].reply_teacher) {
            return res.send({
                status: 0,
                message: '获取成功',
                data: results
                // })
            })
        } else {
            db.query('select * from teacher where teacher_num=?', results[0].reply_teacher, function (err, result) {
                if (err) return res.cc(err)
                // if (result.length < 1) return res.cc("错误")
                result.forEach(val => {
                    results.forEach(value => {
                        if (val.teacher_num == value.reply_teacher)
                            Object.assign(value, { replyTeacher_name: val.teacher_name })
                    })
                })
                return res.send({
                    status: 0,
                    message: '获取成功',
                    data: results
                    // })
                })
                // console.log(results);
            })
        }
    })
}

//获取个人上传的论文 
exports.get_paper = function (req, res) {
    db.query('select * from article where article_dele=0 and article_studentId=?', req.user.username, function (err, results) {
        if (err) return res.cc(err)
        if (results.length < 1) return res.cc('你还没上传呢')
        res.send({
            status: 0,
            message: '你的论文来了哦',
            data: results
        })
    })
}

// 删除个人上传的论文
exports.dele_paper = function (req, res) {
    // console.log(req.body);
    db.query('update article set article_dele=1 where id=?', req.body.id, function (err, results) {
        if (err) return res.cc(err)
        if (results.length < 1) return res.cc('没有删除哦')
        res.send({
            status: 0,
            message: '删除成功了哦',
        })
    })
}


//上传论文
exports.add_paper = function (req, res) {
    // console.log(req.body);
    // console.log(req.user);
    //处理文件的信息对象 
    const articleInfo = {
        //标题，内容，发布状态，所属内容
        article_type: req.body.article_type,
        article_studentId: req.user.username,
        article_date: new Date(),
        article_name: req.file.originalname,
        article_location: path.join('/upload', req.file.filename)
    }
    if (!req.file || req.file.fieldname !== 'file')
        return res.cc('文件必上传')
    db.query('insert into article set ?', articleInfo, function (err, results) {
        if (err) return res.cc(err)
        if (results.affectedRows != 1) return res.cc('上传失败')
        res.send({
            status: 0,
            message: '上传成功'
        })
    })
    // })

}


// 查看自己的毕业设计的分数
exports.get_score = function (req, res) {
    db.query('select * from article where article_dele=0 and article_type="终稿" and article_studentId=?', req.user.username, function (err, results) {
        if (err) return res.cc(err)
        if (results.length < 1) return res.cc('获取失败')
        res.send({
            status: 0,
            message: '你的分数来了',
            data: results
        })
    })
}

//获取答辩小组的个人信息
exports.get_defence = function (req, res) {
    db.query('select * from student_group where student_num=?', req.user.username, function (err, results) {
        if (err) return res.cc(err)
        if (results.length < 1) return res.cc('获取失败')
        res.send({
            status: 0,
            message: '你的答辩小组来了',
            data: results
        })
    })
}